<?php
require_once '../../includes/global.php';
if (get_magic_quotes_gpc()) {
    function stripslashes_deep($value)
    {
        $value = is_array($value) ?
                    array_map('stripslashes_deep', $value) :
                    stripslashes($value);

        return $value;
    }

    $_POST = array_map('stripslashes_deep', $_POST);
    $_GET = array_map('stripslashes_deep', $_GET);
    $_COOKIE = array_map('stripslashes_deep', $_COOKIE);
    $_REQUEST = array_map('stripslashes_deep', $_REQUEST);
}
//error_reporting(E_ALL);
//得到配置文件
$wcfg=Dga('wcfg',"web like '".$sitename."' and lg like 'zh'");
$wcfg=json_decode($wcfg[wcfg],true);

$dcfg=Dga('dcfg',"id like '".$_POST[did]."'");
$dcfg=json_decode($dcfg[dcfg],true);
//检测多语言屏蔽
$_GET['did']=$_POST['did'];
if (array_key_exists($_GET['did'], $wcfg['ping'])) {
	$wcfg['num'] = 1;
	$temp = $wcfg['lang'][$wcfg['ping'][$_GET['did']]];
	unset ($wcfg['lang']);
	$wcfg['lang'][$wcfg['ping'][$_GET['did']]] = $temp;
}

$special_files=array();

//print_r($_FILES);
//Jc('p');exit;
//Jc('f');
//Jc($wcfg);Jc($_POST['update_lang']);exit;
//exit;
foreach($wcfg['lang'] as $lkey =>$lg){

        if(array_search($lkey, $_POST['update_lang'])===false){
            continue;
        }
		foreach($dcfg[structure] as $dkey =>$dvalue){

            /**
             * 检测是否唯一
             */
            if($dvalue['only']==1){
                $only='';   //初始化
                $only = Dga($dcfg[name],"del!=1 and {$dkey}='{$_POST[$lkey][$dkey]}'");
                if($only!='' && $only['id']!=$_POST[$lkey]['id']){
                    Jt('-1',"<b>{$dcfg['structure'][$dkey]['COMMENT']}</b>的值在系统中已存在",3);
                    exit;
                }
            }
			switch ($dvalue['ftype']){
				case "phpfile":{
				Fw($_SERVER['DOCUMENT_ROOT'].TMPE_PATH."admin/db/".$lkey.$_POST[$lkey]['gid'].".php",(($_POST[$lkey][$dkey])));
				require_once ( $_SERVER['DOCUMENT_ROOT'].TMPE_PATH."admin/db/".$lkey.$_POST[$lkey]['gid'].".php");
				$_POST[$lkey][$dkey]=json_encode($tjson);
				unset($tjson);
				break;
				}
				case "textarea":{
				$_POST[$lkey][$dkey]=html_entity_decode(stripslashes($_POST[$lkey][$dkey]));
				break;
				}
				case "ubb":{
				$_POST[$lkey][$dkey]=html_entity_decode(stripslashes($_POST[$lkey][$dkey]),null,"utf-8");
				break;
				}
				case "select":{
				sort($_POST[$lkey][$dkey]);
				//$_POST[$lkey][$dkey]="".implode(',',$_POST[$lkey][$dkey]);
				break;
				}
				case "checkbox":
				case "dcheckbox":{
				sort($_POST[$lkey][$dkey]);
				$_POST[$lkey][$dkey]="".implode(',',$_POST[$lkey][$dkey]).",";
				break;
				}
				case "md5str":{
				$infot=Dga($dcfg[name]," id like '".$_POST[$lkey][id]."'");
					if($infot[$dkey]!=$_POST[$lkey][$dkey]){
				$_POST[$lkey][$dkey]=md5($_POST[$lkey][$dkey]);
					}
				break;
				}
				case "ssx":{
					foreach($dvalue['zd'] as $xxskey){
					$_POST[$lkey][$xxskey]=$_POST[$lkey][$xxskey];
					}
					break;
				}
				case "fileup":{
						for($filenum=1;$filenum<=$dvalue['filenum'];$filenum++){
						$cp=1;
						//复制文件
						if(!$_FILES[$lkey]['name'][$dkey][$filenum]){
							//Ja($lg.$dvalue['COMMENT']."字段 第".$filenum."个上传域为空 跳过!");
							$cp=0;
						}
					$otype=substr(strtolower($_FILES[$lkey]['name'][$dkey][$filenum]),-3);
					if(!in_array($otype,$dvalue['format'])){
							//Ja($lg.$dvalue['COMMENT']."字段 第".$filenum."个上传类型不符 跳过!");
							$cp=0;
					}
					if($_FILES[$lkey]['size'][$dkey][$filenum]>$dvalue['filesize']){
							//Ja($lg.$dvalue['COMMENT']."字段 第".$filenum."个上传大小超过".$dvalue['filesize']."字节(bit)跳过!");
							$cp=0;
					}
				//复制文件
				if($cp){
				$filename="../../uploads/pic/".$lkey.$_POST[$lkey]['gid'].$filenum.".".$otype;
				$tempname[$lkey][]=$lkey.$_POST[$lkey]['gid'].$filenum.".".$otype;
				@unlink($filename);
				$copymes=copy($_FILES[$lkey]['tmp_name'][$dkey][$filenum],$filename);
				}
				//缩略图
					if(in_array($otype,$dvalue['thumb'])){
						createthumb($_FILES[$lkey]['tmp_name'][$dkey][$filenum],".".$otype,$lkey.$_POST[$lkey]['gid'].$filenum);
					}
				//图片水印
					if($dvalue['watermarkpic']){
						if(in_array($otype,$dvalue['watermark'])){
						 imageWaterMark($filename,$dvalue['picposition'],$_SERVER['DOCUMENT_ROOT'].TMPE_PATH.$dvalue['watermarkpic'],"",5,"#FFFFFF");
						 }
					}
				//文字水印
					if($dvalue['watermarktxt']){
						if(in_array($otype,$dvalue['watermark'])){
						//echo $filename.",".$dvalue['txtposition'].",\"\",".$dvalue['watermarktxt'].",5,\"#FFFFFF\"";
			imageWaterMark($filename,$dvalue['txtposition'],"",$dvalue['watermarktxt'],5,"#FFFFFF");
							 }
					}


					}
				$tempic=Dga($dcfg['name']," gid ='".$_POST[$lkey]['gid']."' and lg like '".$lkey."' ");
					$tempic=explode(',',$tempic[$dkey]);
					//Jc($tempic);
					//Jc($tempname);
					foreach($tempic as $picvalue){
							$num=substr($picvalue,-5,1);
							if($tempname[$lkey][$num]==''){
								$tempname[$lkey][$num]=$picvalue;
							}
					}

				$_POST[$lkey][$dkey]=implode(",",$tempname[$lkey]);
				break;
				}
				/***********************
				* 这个是可以删除原来的图像
				*
				*/
				case "fileupBic":{
					if(!class_exists('image_bic')){
						include BIC_ROOT.'includes/class/class_image.php';
					}

					$path=$dvalue['path']==''?"../../uploads/pic/":$dvalue['path'];

					$filter_file = array('teach_addr');	//要过滤的类型,这里只能手动,因为这里涉及到的一些文件都要手动进行重新写程序

					for($filenum=1;$filenum<=$dvalue['filenum'];$filenum++){

						/*
						 * 过滤各别特别的文件,[主要是对上传文件类型来说 ,在format里设置的(可以在其他类型里设置)]
						 * 要处理过滤的会在最后面进行操作
						 * */
						if(in_array($dvalue['format'][0],$filter_file)){
							$n['file_name']=$_FILES[$lkey]['tmp_name'][$dkey][$filenum];
							$n['file_type']=$dvalue['format'][0];
							array_push($special_files,$n);
							continue;
						}

						$cp=1;
						//复制文件
						if(!$_FILES[$lkey]['name'][$dkey][$filenum]){
							//Ja($lg.$dvalue['COMMENT']."字段 第".$filenum."个上传域为空 跳过!");
							$cp=0;
						}
						$otype=substr(strtolower($_FILES[$lkey]['name'][$dkey][$filenum]),-3);
						if(!in_array($otype,$dvalue['format'])){
								//Ja($lg.$dvalue['COMMENT']."字段 第".$filenum."个上传类型不符 跳过!");
								$cp=0;
						}
						if($_FILES[$lkey]['size'][$dkey][$filenum]>$dvalue['filesize']){
								//Ja($lg.$dvalue['COMMENT']."字段 第".$filenum."个上传大小超过".$dvalue['filesize']."字节(bit)跳过!");
								$cp=0;
						}
						//复制文件
						if($cp){
							$tImgName=Bic();
							$filename=$path.$lkey.$tImgName.$filenum.".".$otype;
							$tempname[$lkey][]=$lkey.$tImgName.$filenum.".".$otype;
							//@unlink($filename);
							$copymes=copy($_FILES[$lkey]['tmp_name'][$dkey][$filenum],$filename);
							$_POST[$lkey][$dkey]=$lkey.$tImgName.$filenum.".".$otype;
						}

                        //水印
                        $watermark_sql="SELECT * FROM `lm_watermark` WHERE 1 and id=1 and lg='zh'";
                        $watermark=Dz($watermark_sql);
                        if($watermark[0]['switch']==1 /*&& in_array($_REQUEST['did'],$watermark_filter)*/){
                        //Jc($watermark);exit;
							if(in_array($otype,array("jpeg","gif","png","jpg","bmp"))){
								//Jc($watermark);exit;
								$pica=$watermark[0]['img'];
                                //imageWaterMark($filename,$dvalue['picposition'],$_SERVER['DOCUMENT_ROOT'].TMPE_PATH.$dvalue['watermarkpic'],"",5,"#FFFFFF");
                                imageWaterMark($filename,$watermark[0]['position'],"../../uploads/pic/{$pica}");
							 }
						}

						//缩略图
                        if($cp){
                            if(in_array($otype,array("jpeg","gif","png","jpg","bmp"))){
                                $thumb=new image_bic($filename);
                                //固定生成一缩略图
                                $default_thumb_pic="../../uploads/thumb/".$lkey.$tImgName.$filenum.".".$otype;
                                $thumb->thumb(300,200,$default_thumb_pic);

                                //生成配置缩略图
                                if ($dvalue['thumb_open'] && is_file($filename)){
                                    foreach ($dvalue['thumb'] as $tbK=>$tbV){
                                        $thumb_num=$tbK;
                                        $filename=$path."thumb{$thumb_num}_".$lkey.$tImgName.$filenum.".".$otype;
                                        if($tbV['thumb_open']==1){
                                            $thumb->thumb($tbV['width'],$tbV['height'],$filename);
                                        }
                                    }
                                }
                            }
                        }
						//图片水印
						if($dvalue['watermarkpic']){
							if(in_array($otype,$dvalue['watermark'])){
							 imageWaterMark($filename,$dvalue['picposition'],$_SERVER['DOCUMENT_ROOT'].TMPE_PATH.$dvalue['watermarkpic'],"",5,"#FFFFFF");
							 }
						}
						//文字水印
						if($dvalue['watermarktxt']){
							if(in_array($otype,$dvalue['watermark'])){
								//echo $filename.",".$dvalue['txtposition'].",\"\",".$dvalue['watermarktxt'].",5,\"#FFFFFF\"";
								imageWaterMark($filename,$dvalue['txtposition'],"",$dvalue['watermarktxt'],5,"#FFFFFF");
							}
						}
						if($_FILES[$lkey]['tmp_name'][$dkey][$filenum]!=''){
							$tempic=Dga($dcfg['name']," gid ='".$_POST[$lkey]['gid']."' and lg like '".$lkey."' ");
							$tempic=explode(',',$tempic[$dkey]);
							//Jc($tempic);
							//Jc($tempname);
							foreach($tempic as $picvalue){
								//ja($picvalue);
								$filename=$path.$picvalue;
								//Ja($filename);
								@unlink($filename);
								for ($i=1;$i<=5;$i++){
									@unlink($path."thumb{$i}_".$picvalue);
								}
							}
						}


					}
				//Ja($lkey.$_POST[$lkey]['gid'].$filenum.".".$otype);
				//$_POST[$lkey][$dkey]=$lkey.$_POST[$lkey]['gid'].$filenum.".".$otype;
				break;
				}
			}
			/* 插件功能
			 * 思想:将临时或在函数库中编写的函数加入进来,处理本条数据
			 * Author by Bic.2009.02.14
			 */
			if(trim($dvalue['plug'])!=''){
				$_POST[$lkey][$dkey]=$dvalue['plug']($_POST[$lkey][$dkey]);
			}
		}

		//操作数据库
		//Jc($_POST);
		//exit;
		$_POST[$lkey]['sortid']=$_POST['sortid'];
		if($_POST[$lkey][id]==''){
			unset($_POST[$lkey][id]);
			Di($dcfg[name],$_POST[$lkey]);
			foreach($dcfg[structure] as $dkey =>$dvalue){
				switch ($dvalue['ftype']){
				case "back":{
					backdselect(mysql_insert_id(),$dcfg[name]);
					break;
				}
				}
			}
		}else{
		    $_POST[$lkey]['del'] = 0;
			Du($dcfg[name],$_POST[$lkey],'id',$_POST[$lkey][id]);
			foreach($dcfg[structure] as $dkey =>$dvalue){
				switch ($dvalue['ftype']){
				case "back":{
					backdselect($_POST[$lkey][id],$dcfg[name]);
					break;
				}
				}
			}
		}
		if(count($special_files)>0){
			require BIC_ROOT.'includes/class/excel_class.php';
			$start_index='';
			foreach($special_files as $spK=>$spV){
				switch ($spV['file_type']){
					/*异地教学上传csv*/
					case 'teach_addr':
						$id=$_POST[$lkey][id]!=''?$_POST[$lkey][id]:mysql_insert_id();
						$info=Dga('teach_addr',"id={$id}");
						//error_reporting(E_ALL);
						//Read_Excel_File($spV['file_name'],$result);
						$result=file($spV['file_name']);
						//$result = array_filter($result,'cf');
						//$result = array_filter($result);
						foreach($result as $fK=>$fV){
							$tmp=explode(',',$fV);
							foreach($tmp as &$tmp2){
								$tmp2=iconv("gb2312","UTF-8",$tmp2);
							}
							switch($tmp[0]){
								case '1':
									$class['title']=$tmp[1];
									$class['addtime']=time();
									$class['bdb']=31;
									$class['bdid']=$info['gid'];
									$class['gid']=Bic();

									Di('teach_class',$class);
									$teach_course_id=mysql_insert_id();
									$class_info=Dga('teach_class',"id={$teach_course_id}");
									break;
								case '2':
									$course['date']=$tmp[1];
									$course['content']=$tmp[2];
									$course['class']=$tmp[3];
									$course['addr']=$tmp[4];
									$course['addtime']=time();
									$course['bdb']=32;
									$course['bdid']=$class_info['gid'];
									$course['gid']=Bic();
									Di('teach_course',$course);
									break;
							}
						}
						//Jc($_FILES[$lkey]['tmp_name'][$dkey][$filenum]);
						break;
				}
			}
		}
}
//echo "OK;";
//exit;
Jt($_SERVER['HTTP_REFERER'],'操作成功');
?>
